﻿<%@ WebHandler Language="C#" Class="FileHandler" %>

using System;
using System.Web;
using System.IO;
using System.Collections.Generic;
using System.Collections;

public class FileHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        ArrayList typelist = new ArrayList() { "*.jpg", "*.png" };
        string pathA = HttpContext.Current.Server.MapPath("/upload/image/");
        string path = @pathA;
        context.Response.CacheControl = "no-cache";
        context.Response.Write(JsonHelper.ToJson(FindFile(path, typelist)) + "|" + list.Count);
        context.Response.Flush();
        context.Response.End();
    }
    List<String> list = new List<string>();
    public List<string> FindFile(string SourcePath, ArrayList typelist)
    {

        //遍历文件夹
        DirectoryInfo theFolder = new DirectoryInfo(SourcePath);
        //遍历子文件夹
        DirectoryInfo[] dirInfo = theFolder.GetDirectories();
        FileInfo[] fileInfo;
        foreach (string type in typelist)
        {
            foreach (DirectoryInfo NextFolder in dirInfo)
            {
                HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);
                //list.Add(NextFolder.ToString());
                fileInfo = NextFolder.GetFiles(type, SearchOption.AllDirectories);
                foreach (FileInfo NextFile in fileInfo)  //遍历文件
                {
                    list.Add(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "/upload/image/" + NextFile.Directory.Name + "/" + NextFile.Name);
                }
            }
        }

        return list;
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}